package resoft.jeasyform.components;

import charvax.swing.JTextField;
import resoft.jeasyform.event.*;
import charva.awt.Toolkit;
import charva.awt.Color;

/**
 * <p>description of TextField</p>
 * User: albert
 * Date: Aug 26, 2007
 * Time: 7:51:35 PM
 */
public class TextField extends InputField {

    public String getValue() {
        if(jTextField!=null) {
            this.value = jTextField.getText();
        }
        return value;
    }

    public void setValue(String value) {
        if(value==null) {
            value="";
        }
        this.value = value;
        if(jTextField!=null) {
            jTextField.setText(value);
        }
        //TextFieldUtil.setValue(jTextField,this,value);
    }


    public void setValidation(String validation) {
        this.validation = validation;
    }

    /**
     * to swing object
     */
    public charva.awt.Component getSwingObject() {
        if(jTextField!=null) {
            return jTextField;
        }
        jTextField = new JTextField(getValue());
        jTextField.setBounds(getY(),getX(),getY() + getHeight(),getX() + getWidth());
        jTextField.setVisible(visible);
        jTextField.setPaddingChar('_');
        jTextField.setColumns(getWidth());

        jTextField.addKeyListener(new FixedLengthEventHandler(this,getMaxLength()));
        if(validation!=null) {
            if(validation.equalsIgnoreCase("currency")) {
                jTextField.addKeyListener(new CurrencyValidationEventHandler(this));
            }
        }

        Toolkit.getDefaultToolkit().blankBoxNative(5,10,20,20, Color.black.getCursesColor());

        //jTextField.addKeyListener(new );

        jTextField.addActionListener(new InputFieldEnterPressedEventAdapter(getParentWindow(),this,getPostEventChain()));
        return jTextField;
    }

     /**
     * hide
     */
    public void hide() {
        if(jTextField!=null) {
            jTextField.setVisible(false);
        }
        setAcceptFocus(false);
        visible = false;
    }

    /**
     * show
     */
    public void show() {
        if(jTextField!=null) {
            jTextField.setVisible(true);
        }
        setAcceptFocus(true);
        visible = true;
    }


    /**
     * request focus
     * */
    public void requestFocus() {
        jTextField.requestFocus();
        getPreEvent().execute(getParentWindow(),this,EventType.TextField_PreEvent);
    }

    private String value = "";

    private String validation = "";

    private JTextField jTextField = null;

    private boolean visible = true;
}

